﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Data.SqlClient;

namespace HYWJ
{
    public partial class examForm : Office2007Form
    {
        
        public 
        SqlCommand command = null;
        SqlDataReader dataReader = null;

        public examForm()
        {
            InitializeComponent();
        }

        public void examForm_Load(object sender, EventArgs e)
        {
            //连接数据库
            DataBase.OpenDataBase();
            this.bt_before.Enabled = false;
            QuestionCls.InitRnd(QuestionCls.StuSubjectId);
            RefreshCls();
        }

        //上一题
        private void bt_before_Click(object sender, EventArgs e)
        {
            if (QuestionCls.QuestionIndex <= 0)
            {
                this.bt_before.Enabled = false;
                this.bt_Next.Enabled = true;
                MessageBox.Show("已到第一题！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                this.bt_Next.Enabled = true;
                QuestionCls.QuestionIndex--;
                RefreshCls();
            }
        }


        //下一题
        private void bt_Next_Click(object sender, EventArgs e)
        {
            if (QuestionCls.QuestionIndex >= 19)
            {
                this.bt_Next.Enabled = false;
                this.bt_before.Enabled = true;
                MessageBox.Show("已到最后一题！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                this.bt_before.Enabled = true;
                QuestionCls.QuestionIndex++;
                RefreshCls();
            }
        }

        //答题卡按钮事件
        private void button_click(object sender, EventArgs e)
        {
            int no = 0;
            no = Convert.ToInt32(((Button)sender).Tag);
            QuestionCls.QuestionIndex = no;

            RefreshCls();
        }


        //题目显示刷新
        public void RefreshCls()
        {
            rdoOptionA.Checked = false;
            rdoOptionB.Checked = false;
            rdoOptionC.Checked = false;
            rdoOptionD.Checked = false;

            if (QuestionCls.Answers[QuestionCls.QuestionIndex] != "")
            {
                switch (QuestionCls.Answers[QuestionCls.QuestionIndex])
                {
                    case "A":
                        rdoOptionA.Checked = true;
                        break;
                    case "B":
                        rdoOptionB.Checked = true;
                        break;
                    case "C":
                        rdoOptionC.Checked = true;
                        break;
                    case "D":
                        rdoOptionD.Checked = true;
                        break;
                }
            }
            else
            {
                rdoOptionA.Checked = false;
                rdoOptionB.Checked = false;
                rdoOptionC.Checked = false;
                rdoOptionD.Checked = false;
            }
            string sql = "select * from Question where QuestionID=" + QuestionCls.QID[QuestionCls.QuestionIndex];
            command = new SqlCommand(sql, DataBase.connection);
            dataReader = command.ExecuteReader();
            if (dataReader.Read())
            {
                tb_question.Text = dataReader["Question"].ToString();
                rdoOptionA.Text = "A. " + dataReader["OptionA"].ToString();
                rdoOptionB.Text = "B. " + dataReader["OptionB"].ToString();
                rdoOptionC.Text = "C. " + dataReader["OptionC"].ToString();
                rdoOptionD.Text = "D. " + dataReader["OptionD"].ToString();
            }
            dataReader.Close();
            l_question.Text = (QuestionCls.QuestionIndex + 1).ToString();
        }

        //交卷，计算成绩
        private void bt_交卷_Click(object sender, EventArgs e)
        {
            QuestionCls.stuTrueNum = 0;
            int score = 0;
            string sql = "select QuestionID,Answer from Question where subjectId=" + QuestionCls.StuSubjectId;
            SqlCommand command = new SqlCommand(sql, DataBase.connection);
            SqlDataReader dataReader = command.ExecuteReader();
            string answers = "";
            int id = 0;
            while (dataReader.Read())
            {

                answers = dataReader["Answer"].ToString();
                id = (int)dataReader["QuestionID"];
                for (int i = 0; i < QuestionCls.Answers.Length; i++)
                {
                    if (answers == QuestionCls.Answers[i] && QuestionCls.QID[i] == id)
                    {
                        QuestionCls.stuTrueNum++;
                    }
                }
            }
            dataReader.Close();
            score = QuestionCls.stuTrueNum;
            score = score * 5;
            MessageBox.Show("你本次考试的成绩为"+score+"分");


            this.Close();
            
        }

        private void rdoOptionA_Click(object sender, EventArgs e)
        {
            QuestionCls.Answers[QuestionCls.QuestionIndex] = ((RadioButton)sender).Tag.ToString();
        }
    }
}
